<template>
  <el-dialog
    :title="!dataForm.id ? $t('message.add') : $t('message.edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item :label="$t('message.phoneImage')" prop="phoneImage" :class="{ 'is-required': true }">
        <el-upload
          class="upload-demo"
          drag
          :action= this.uploadUrl
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :before-upload="beforeUpload"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">{{$t('message.uploadText')}}<em>{{$t('message.clickUpload')}}</em></div>
          <div class="el-upload__tip" slot="tip">{{$t('message.uploadTip')}}</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="" prop="">
          <img :src="dataForm.phoneImage" alt="" height="200" width="200">
      </el-form-item>
      <el-form-item :label="$t('message.brand')" prop="brandId" :class="{ 'is-required': true }">
          <el-select v-model="dataForm.brandId">
            <el-option v-for="item in brandList" :key="item.id" :label="item.brandName" :value="item.id" />
          </el-select>
      </el-form-item>
      <el-form-item :label="$t('message.model')" prop="phoneModel" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.phoneModel" :placeholder="$t('message.model')"></el-input>
      </el-form-item>
      <!-- <el-form-item :label="$t('message.screenWidth')" prop="width" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.width" :placeholder="$t('message.screenWidth')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.screenHeight')" prop="height" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.height" :placeholder="$t('message.screenHeight')"></el-input>
      </el-form-item> -->
      <el-form-item :label="$t('message.sortId')" prop="sortNum" :class="{ 'is-required': true }">
        <el-input-number v-model="dataForm.sortNum" controls-position="right" :min="0" :label="$t('message.sortId')"></el-input-number>
      </el-form-item>
      <el-form-item :label="$t('message.status')" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">{{$t('message.disable')}}</el-radio>
          <el-radio :label="1">{{$t('message.ok')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('message.pltType')" size="mini" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">{{$t('message.fullScreen')}}</el-radio>
          <el-radio :label="2">{{$t('message.shell')}}</el-radio>
          <el-radio :label="3">{{$t('message.back')}}</el-radio>
          <el-radio :label="4">{{$t('message.cameraLens')}}</el-radio>
          <el-radio :label="5">{{$t('message.oca')}}</el-radio>
          <!-- <el-radio :label="6">{{$t('message.watchScrenn')}}</el-radio>
          <el-radio :label="7">{{$t('message.colorHalfScrenn')}}</el-radio>
          <el-radio :label="8">{{$t('message.colorFullScrenn')}}</el-radio> -->
          <el-radio :label="6">{{$t('message.other')}}</el-radio> -->
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('message.pltMD5')" size="mini" prop="md5" :class="{ 'is-required': true }">
        <el-input v-model="dataForm.md5" :placeholder="$t('message.pltMD5')" disabled ></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.pltFile')" prop="pltUrl" :class="{ 'is-required': true }">
        <el-upload
          class="upload-demo"
          drag
          :action= this.uploadPltUrl
          :on-success="handleUploadPltSuccess"
          :before-upload="beforeUploadPlt"
          :on-remove="removePltFile"
          :file-list= this.fileList
          :limit="1">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">{{$t('message.uploadText')}}<em>{{$t('message.clickUpload')}}</em></div>
          <div class="el-upload__tip" slot="tip">{{$t('message.uploadTip')}}</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.cancle')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        uploadUrl: '',
        uploadPltUrl: '',
        dialogVisible: false,
        dialogImageUrl: '',
        brandList: [],
        fileList: [],
        dataForm: {
          id: 0,
          phoneImage: '',
          brandId: '',
          phoneBrand: '',
          phoneModel: '',
          sortNum: 1,
          status: 1,
          // width: '',
          // height: '',
          type: '',
          pltUrl: '',
          originName: '',
          md5: ''
        },
        dataRule: {
          phoneImage: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          pltUrl: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          brandId: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          phoneModel: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          sortNum: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          // width: [
          //   { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          // ],
          // height: [
          //   { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          // ],
          md5: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.uploadUrl = window.SITE_CONFIG.baseUrl + '/sys/image/uploadFilesToNginx'
        this.uploadPltUrl = window.SITE_CONFIG.baseUrl + '/sys/phone/uploadPltFile'
        this.fileList = []
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
        this.$http({
          url: this.$http.adornUrl(`/sys/phone/queryTotalBrand`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.brandList = data.brandList
          } else {
            this.$message.error(data.msg)
          }
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/phone/queryPhoneModelInfo`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.model.id
              this.dataForm.phoneImage = data.model.phoneImage
              this.dataForm.brandId = data.model.brandId
              this.dataForm.phoneModel = data.model.phoneModel
              this.dataForm.status = data.model.status
              this.dataForm.sortNum = data.model.sortNum
              // this.dataForm.width = data.model.width
              // this.dataForm.height = data.model.height
              this.dataForm.type = data.model.type
              this.dataForm.pltUrl = data.model.pltUrl
              this.dataForm.originName = data.model.originName
              this.dataForm.md5 = data.model.md5
              this.brandList = data.brandList
              this.fileList.push(data.plt)
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/phone/savePhoneModelInfo`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'phoneImage': this.dataForm.phoneImage,
                'phoneModel': this.dataForm.phoneModel,
                'brandId': this.dataForm.brandId,
                // 'width': this.dataForm.width,
                // 'height': this.dataForm.height,
                'sortNum': this.dataForm.sortNum,
                'status': this.dataForm.status,
                'type': this.dataForm.type,
                'pltUrl': this.dataForm.pltUrl,
                'originName': this.dataForm.originName,
                'md5': this.dataForm.md5
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('message.success'),
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleUploadSuccess: function (response) {
        this.dataForm.phoneImage = response.imagePath
        this.dialogImageUrl = response.imagePath
        this.dialogVisible = true
      },
      beforeUpload (file) {
        var test1 = /^image\/(jpeg|png|jpg)$/.test(file.type)
        const isLt5M = file.size / 1024 / 1024 <= 5
        if (!test1) {
          this.$message.error(this.$t('message.caption'))
        }
        if (!isLt5M) {
          this.$message.error(this.$t('message.uploadTip'))
        }
        return test1 && isLt5M
      },
      handleUploadPltSuccess: function (response) {
        console.log(this.fileList)
        this.dataForm.pltUrl = response.plt.url
        this.dataForm.originName = response.plt.name
        this.dataForm.md5 = response.plt.md5
        this.fileList.push(response.plt)
        console.log(this.fileList)
      },
      beforeUploadPlt: function (file) {
        const isLt50M = file.size / 1024 / 1024 <= 50
        if (!isLt50M) {
          this.$message.error(this.$t('message.uploadTip'))
        }
        const isTest = (this.dataForm.pltUrl === '')
        if (!isTest) {
          this.$message.error(this.$t('message.reUpload'))
        }
        return isTest && isLt50M
      },
      removePltFile: function (file) {
        console.log('remove file')
        this.dataForm.pltUrl = ''
        this.fileList.pop(file)
      }
    }
  }
</script>
